
///////////////////////////////////////////////////////////////////////////
//	Class: Pipe - Create a pipe with length is NUM_PIPE_LINE which is set in ADUConstant.h
//	Dev: DungNT
//	Usage: 	
//		Pipe* mPipe = new Pipe();
//		mPipe->createPipe(_layer);
///////////////////////////////////////////////////////////////////////////


#ifndef __ADU_PIPE_H__
#define __ADU_PIPE_H__

#include "cocos2d.h"
#include "ADUConstant.h"
#include "ADUWater.h"
#include "Utils.h"
//#include "ADUFlow.h"
USING_NS_CC;
using namespace std;

class Flow;

class PipeHead : public Sprite {
	public:
		PipeHead() {
            this->setTag(TAG_PIPEHEAD);
        };
		~PipeHead(){};
		static PipeHead* createPipeHead(const string& file);
		bool 			 initPipeHeadWithFile(const string& file);
        void    AddPhysic();
	private:
		EventListenerTouchOneByOne* mTouchListener;
        b2Body* mBody;
};


class PipeCircle : public Sprite {
	public:
		PipeCircle(){ mFlow = NULL; };
		~PipeCircle(){};
		static PipeCircle* 	createPipeCircle(const string& file);
		void 				enableTouch();
		void 				setPipe(SpriteBatchNode* _pipe) {mPipeLine = _pipe;};
		SpriteBatchNode* 	getPipe() {return mPipeLine;};

		void setPipeHead(PipeHead* _pipe) {
			mPipeHead = _pipe;
			mHeadLen = mPipeHead->getContentSize().width;
		};

		PipeHead* 			getPipeHead() {return mPipeHead;};
		
		virtual bool 		onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);
		virtual void 		onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event);
		virtual void 		onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event);
		virtual void 		onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *event);

		void SetFlow(Flow* flow);
		void SetGamePaused(bool paused){
			mGamePaused = paused;
		}
    
        void UpdateFlowPosition();
        void AdjustPipePostion(float dx, float dy);
        void setWater(Water* water) {this->mWater = water;}

	private:
		EventListenerTouchOneByOne* mTouchListener;
		SpriteBatchNode* 		mPipeLine;
		PipeHead* 				mPipeHead;
		Vec2					mStartPosHead;
		bool					mPipeHeadTouch;
		bool					mCircleTouch;
		Flow* 					mFlow;
		float					mHeadLen;
		bool 					mGamePaused;
		Water*					mWater;
		Sprite* 				mHand;
};




class Pipe : public Node {
	public:
		Pipe(){};
		~Pipe(){};
		void createPipe(Node* _layer);
		Vec2 getPipeHeadPosition() { return mPipeHead->getPosition();};
		Size getSizePipeHead() {return mPipeHead->getContentSize();}

		void SetFlow(Flow* flow){
			mPipeCircle->SetFlow(flow);
		};

		void SetGamePaused(bool paused){
			mPipeCircle->SetGamePaused(paused);
		}

		bool isPipeTouched(Vec2 point);
        void Update(float dt);
        void setWater(Water* water) {this->mPipeCircle->setWater(water);}
		void follow(Node* follower, Node* target) ;
		void followDuck(Node* duck);

	private:
		PipeCircle* 		mPipeCircle;
		PipeHead* 			mPipeHead;
		Sprite* 			mPipeSeg[NUM_PIPE_LINE];		// pipe segment
		SpriteBatchNode* 	mPipeLine;
		Vec2 				mLastSegPos;
		
};

#endif
